<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>symbol装载遍历函数</h1>
    <p>在ES6中系统内置了很多全局的symbol常量，用于一些特殊的属性。比如：遍历函数（[Symbol.iterator]）</p>
    <script>
        let arr = [1,2,3,4];

        
        // 数组是可以for...of遍历的
        for (let v of arr.values()) {
            console.log(v);
        }
        
       let fun1 = arr.values;
       let fun2 = arr[Symbol.iterator];
        console.log(fun1 == fun2); // true

        // 如果一个对象有[Symbol.iterator]属性，那么该对象就可以进行遍历
        // 但是：我们必须将[Symbol.iterator]设置为“生成函数”。

        let obj = {};
        obj[Symbol.iterator] = function* gen1() {
            yield ["name","奉先"];
			yield ["age",34];
			yield ["sex","男"];
			return "结束";
        }

        arr = [...obj];
        console.log(arr);

        let obj2 = Object.fromEntries(obj);
        console.log(obj2);

        for (let v of obj) {
            console.log(v);
        }

    </script>
</body>
</html>